package com.li.sssp.handle;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.li.sssp.entity.Employee;
import com.li.sssp.imp.Json;
import com.li.sssp.inter.IJson;
import com.li.sssp.server.ActorService;
import com.li.sssp.server.DepartmentService;
import com.li.sssp.server.EmployeeService;

/**
 * 人员业务的controller
 * 
 * @author guazi
 * @version 0.1
 * @date 2016/12/1
 */

@Controller
@RequestMapping("/user")
public class UserHandle {

	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private ActorService actorService;

	private IJson js;

	/**
	 * 修改之前赋值
	 * 
	 * @param id
	 * @param map
	 */
	@ModelAttribute
	public void getEmployee(@RequestParam(value = "id", required = false) Integer id, Map<String, Object> map) {
		if (id != null) {
			Employee employee = employeeService.getById(id);
			employee.setDepartment(null);
			employee.setActor(null);
			map.put("employee", employee);
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "Del/{id}")
	public String del(@PathVariable(value = "id") Integer id) {
		employeeService.delById(id);
		return "redirect:/user/list";
	}

	/**
	 * 修改保存
	 * 
	 * @param employee
	 * @return
	 */
	@RequestMapping(value = "update/update_save", method = RequestMethod.POST)
	public String update_save(Employee employee) {
		employeeService.Save(employee);
		return "redirect:/user/list";
	}

	/**
	 * 修改界面初始化
	 * 
	 * @param Str_id
	 * @param map
	 * @return 返回update.js
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String update_put(@PathVariable(value = "id") String Str_id, Map<String, Object> map) {
		Integer id;
		try {
			id = Integer.parseInt(Str_id);
		} catch (Exception e) {
			return "list";
			// TODO: handle exception
		}
		map.put("actors", actorService.getAll());
		map.put("employee", employeeService.getById(id));
		map.put("departments", departmentService.getAll());
		return "user/update";
	}

	/**
	 * 
	 * 判断是否唯一
	 * 
	 * @param lastName
	 * @param response
	 */
	@RequestMapping(value = "isOnly", method = RequestMethod.POST)
	public void isOnly(@RequestParam(value = "lastName") String lastName, HttpServletResponse response) {

		js = new Json();
		if (employeeService.getByLastName(lastName) != null) {

			js.renderData(response, "有");
		} else {
			js.renderData(response, "没有");
		}

	}

	/**
	 * 
	 * 修改界面的初始化
	 * 
	 * @param Str_id
	 * @param map
	 * @return
	 */

	@RequestMapping(value = "/input/{id}", method = RequestMethod.GET)
	public String input(@PathVariable(value = "id") String Str_id, Map<String, Object> map) {

		// 1 判断id是正常
		int id = 0;
		try {
			id = Integer.parseInt(Str_id);
		} catch (Exception e) {
			// TODO: handle exception
			return "list";
		}
		// 2添加map

		map.put("employee", employeeService.getById(id));
		map.put("departments", departmentService.getAll());

		// 3 返回
		return "user/input";
	}

	/**
	 * 保存
	 * 
	 * @param employee
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Employee employee, Map<String, Object> map) {
		Date creatTime = new Date();
		employee.setCreateTime(creatTime);
		employeeService.Save(employee);
		return "redirect:list";
	}

	/**
	 * 
	 * 添加初始化界面
	 * 
	 * @param map
	 * @return
	 */

	@RequestMapping(value = "input", method = RequestMethod.GET)
	public String input(Map<String, Object> map) {
		map.put("actors", actorService.getAll());
		map.put("departments", departmentService.getAll());
		map.put("employee", new Employee());

		return "user/input";
	}

	/**
	 * 列表界面
	 * 
	 * @param pageNo
	 *            第几页
	 * @param pageIndex
	 *            每页多少条
	 * @param map{"employees"}
	 * @return list.jsp
	 * @author guazi
	 * @version 0.1 2016-10-27
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String employeeList(@RequestParam(value = "search", required = false, defaultValue = "") String lastName,
			@RequestParam(value = "pageNo", required = false, defaultValue = "1") String str_pageNo,
			@RequestParam(value = "pageIndex", required = false, defaultValue = "5") String str_pageIndex,
			HttpServletRequest request, Map<String, Object> map) {

		// 1提取参数
		int pageNo = 1;
		int pageIndex = 5;
		//获取当前url
		String url = getUrl(request);
		try {
			pageNo = Integer.parseInt(str_pageNo);
			pageIndex = Integer.parseInt(str_pageIndex);
		} catch (Exception e) {
			// TODO: handle exception
		}

		// 2map增加
		// Page<Employee> p = employeeService.getALLForPage(pageNo, pageIndex);

		Page<Employee> p = employeeService.getLastNameForPage(lastName, pageNo, pageIndex);
		map.put("employees", p);
		map.put("url", url);
		map.put("pageNo", pageNo);
		// 3返回
		// return "/list2";
		return "user/list";
	}

	/**
	 * 获取当前url功能
	 * 
	 * @param getServletPath
	 *           当前url
	 * @param getQueryString()
	 *            获取url后查询参数
	 *@param url
	 *            完整的当前url
	 * @author guazi
	 * @version 0.1 2016-10-27
	 */
	public String getUrl(HttpServletRequest request) {
		String url = request.getServletPath();
		if (request.getQueryString() != null) {
			url += "?" + request.getQueryString();
		}
		url = url.replaceFirst("/", "");
		return url;
	}
}
